package com.huaruan.youdi.utils;

import android.content.SharedPreferences;
import android.util.Base64;


import com.huaruan.youdi.base.BaseApplication;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

public class PreferenceUtil {
    public static final String NAME = "preference_wohuo";
    private static SharedPreferences preferences =  BaseApplication.getInstance().getContext()
            .getSharedPreferences(NAME, 0);

    public static void putString(String key, String value) {
        preferences.edit().putString(key, value).commit();
    }

    public static String getString(String key, String defValue) {
        return preferences.getString(key, defValue);
    }

    public static void putBoolean(String key, boolean value) {
        preferences.edit().putBoolean(key, value).commit();
    }

    public static boolean getBoolean(String key, boolean defValue) {
        return preferences.getBoolean(key, defValue);
    }

    public static void putInt(String key, int value) {
        preferences.edit().putInt(key, value).commit();
    }

    public static int getInt(String key, int defValue) {
        return preferences.getInt(key, defValue);
    }

    public static void remove(String key) {
        preferences.edit().remove(key).commit();
    }

    public static void putObject(String key, Object object) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(baos);
            out.writeObject(object);
            String objectVal = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            putString(key, objectVal);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static <T> T getObject(String key, Class<T> clazz) {
        if (preferences.contains(key)) {
            String objectVal = preferences.getString(key, null);
            byte[] buffer = Base64.decode(objectVal, Base64.DEFAULT);
            ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(bais);
                T t = (T) ois.readObject();
                return t;
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bais != null) {
                        bais.close();
                    }
                    if (ois != null) {
                        ois.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static SharedPreferences getPreferences() {
        return preferences;
    }

    public static class PreferenceKeys {
        public static final String UID = "UID";
        public static final String PHONE = "PHONE";
        public static final String AVATAR = "AVATAR";
        public static final String NAME = "NAME";
        public static final String MAIN_SHOW = "MAIN_SHOW";
        public static final String TOKEN = "TOKEN";
        public static final String SHOP_ID = "SHOP_ID";
        public static final String SEARCH_HISTORY = "SEARCH_HISTORY";
        public static final String LON = "LON";
        public static final String LAT = "LAT";
        public static final String FIRST_PROVINCE = "FIRST_PROVINCE";
        public static final String FIRST_CITY = "FIRST_CITY";
        public static final String FIRST_DISTRICT = "FIRST_DISTRICT";
        public static final String SHOP_STATUS = "SHOP_STATUS";
        public static final String SHOP_ADDRESS = "SHOP_ADDRESS";
        public static final String SEARCH_VIDEO = "SEARCH_VIDEO";
        public static final String SEARCH_PRODUCT = "SEARCH_PRODUCT";
        public static final String VERSION = "VERSION";
    }
}
